package org.deng.learn.jedis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import javax.swing.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author 邓力宾
 * @Desc
 * @since 2020/6/9 下午4:13
 */
public class JedisApi {
    private static final String REDIS_SERVER_URL = "127.0.0.1";
    private static final int REDIS_SERVER_PORT = 6379;
    private static Jedis jedis;
    static {
        jedis = new Jedis(REDIS_SERVER_URL, REDIS_SERVER_PORT);
    }

    /**
     * api测试
     */
    private static void testKey(){
        System.out.println("清空数据:"+jedis.flushDB());
        System.out.println("判断某个key是否存在:"+jedis.exists("name"));
        System.out.println("新增:"+jedis.set("name", "denglibin"));
        Set<String> keys = jedis.keys("*");
        System.out.println("所有的key:"+keys);
        System.out.println("数据类型:"+jedis.type("name"));
        System.out.println("删除:"+jedis.del("name"));
    }

    /**
     * 超时时间
     */
    private static void testExpire(){
        jedis.setex("age", 10, "19");
        jedis.expire("age", 20); // 重新设置过期时间
        System.out.println("倒计时时间:" + jedis.pttl("age")); // 毫秒
    }

    /**
     * 操作字符串
     */
    private static void testString(){
        jedis.flushDB();
        System.out.println(jedis.set("key1", "value1"));
        System.out.println("追加："+jedis.append("key1", "value1_2"));
        System.out.println("追加后："+jedis.get("key1"));
        System.out.println("增加多个键值对："+jedis.mset("key1","value1", "key2","value2", "key3", "value3"));
        System.out.println("获取多个键值对："+jedis.mget("key1", "key2", "key3"));
        System.out.println("删除多个键值对："+jedis.del("key1", "key2", "key3"));
        System.out.println("再次获取多个键值对："+jedis.mget("key1", "key2", "key3"));
        jedis.flushDB();

        System.out.println("不存在才添加："+jedis.setnx("key1", "value1"));
        System.out.println("不存在才添加："+jedis.setnx("key1", "value1_2"));
        System.out.println("不存在才添加-获取："+jedis.get("key1"));
        System.out.println("获取原值再更为新值："+jedis.getSet("key1", "value1-2"));
        System.out.println("获取："+jedis.get("key1"));
    }

    /**
     * 操作List
     */
    private static void testList(){
        jedis.flushDB();
        System.out.println("左边进");
        jedis.lpush("names", "wukong", "lisi", "张三", "小明");
        System.out.println("names长度:"+jedis.llen("names"));
        System.out.println("names内容-左边出:"+jedis.lrange("names", 0, -1));
        jedis.lpush("nums", "1", "4", "2", "5", "18", "8", "6");
        System.out.println("排序:"+jedis.sort("nums"));
        System.out.println(jedis.lrange("nums", 0, -1));
        for (int i = 0; i < 10; i++){
            System.out.println(jedis.rpop("nums")); // 右边出
        }

    }

    /**
     * 原子自增
     */
    private static void testIncr(){
        //jedis.set("age", "10");
        System.out.println(jedis.incr("age"));
        System.out.println(jedis.incrBy("age", 10));
    }

    /**
     * 操作set
     */
    private static void testSet(){
        jedis.flushDB();
        jedis.sadd("nums", "1", "2", "3", "3", "2", "9", "8");
        jedis.sadd("nums2", "1", "2", "3", "3", "2" ,"5", "7");
        System.out.println("nums:"+jedis.smembers("nums"));
        System.out.println("nums2:"+jedis.smembers("nums2"));
        System.out.println("交集："+jedis.sinter("nums", "nums2"));
        System.out.println("并集："+jedis.sunion("nums", "nums2"));
        System.out.println("差集："+jedis.sdiff("nums", "nums2"));
        System.out.println("差集："+jedis.sdiff("nums2", "nums"));

    }

    /**
     * 操作hash
     */
    private static void testHash(){
        jedis.flushDB();
        Map<String, String> map = new HashMap<>();
        map.put("name1", "张三");
        map.put("name2", "李四");
        map.put("name3", "Mary");
        map.put("name4", "Tom");
        jedis.hmset("names", map);
        jedis.hset("names", "name5", "Tomcat");
        System.out.println("所有的键值对:" + jedis.hgetAll("names"));
        System.out.println("所有的键:" + jedis.hkeys("names"));
        System.out.println("所有的值:" + jedis.hvals("names"));
        jedis.flushDB();
    }

    /**
     * 操作zset（有序set）
     */
    private static void testSortSet(){
        jedis.flushDB();
        Map<String, Double> map = new HashMap<>();
        map.put("lisi", 9.8);
        map.put("zs", 8.8);
        map.put("ww", 7.8);
        map.put("cl", 8.5);
        jedis.zadd("name::scores", map);
        jedis.zadd("name::scores", 9.1, "tomcat");
        System.out.println("zset所有元素-升序："+jedis.zrange("name::scores", 0, -1));
        System.out.println("zset所有元素-降序："+jedis.zrevrange("name::scores", 0, -1));
        System.out.println("zset中lisi的分数："+jedis.zscore("name::scores","lisi"));
        System.out.println("zset中lisi的排名-升序："+jedis.zrank("name::scores","lisi"));
        System.out.println("zset中lisi的排名-降序："+jedis.zrevrank("name::scores","lisi"));
    }

    /**
     * 连接池
     */
    public static void testJedisPool(){
        //1 获得连接池配置对象，设置配置项
        JedisPoolConfig config = new JedisPoolConfig();
        // 1.1 最大连接数
        config.setMaxTotal(30);

        //1.2 最大空闲连接数
        config.setMaxIdle(10);

        //获得连接池
        JedisPool jedisPool = new JedisPool(config,REDIS_SERVER_URL,REDIS_SERVER_PORT);


        Jedis jedis=null;
        try {
            //3.获得核心对象
            jedis = jedisPool.getResource();
            //4.设置数据
            jedis.set("name","xinruyi");
            //5.获得数据
            String name = jedis.get("name");
            System.out.println(name);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(jedis!=null){
                jedis.close();
            }
        }

        //虚拟机关闭时，释放pool资源
        if(jedisPool!=null){
            jedisPool.close();
        }
    }


    public static void main(String[] args) {
       // testKey();
      //  testExpire();
        //testString();
        //testList();
        //testIncr();
        //testSet();
       // testHash();
        //testSortSet();
        testJedisPool();
    }
}
